package com.tianyu.model.dto;

import com.tianyu.model.enums.RoState;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.experimental.Accessors;

/**********************************
 * @Author YSW
 * @Description 接口返回通用类型 Ro(Return Object)(请求返回类型)
 * @Date 2021/1/6 - 18:34
 **********************************/

@Data
@Accessors(chain = true)
@Builder
@AllArgsConstructor
public class Ro<T> {

    private Integer state;

    private String message;

    private T content;

    public Ro() {
        setResultEnum(RoState.SUCCESS);
    }

    public Ro(RoState re) {
        this.state = re.getValue();
        this.message = re.getMessage();
    }

    public Ro<T> setResultEnum(RoState re) {
        this.state = re.getValue();
        this.message = re.getMessage();
        return this;
    }
    
    public static Ro<?> error() {
        return Ro.builder()
                .state(RoState.FAIL.getValue())
                .build();
    }

    public static Ro<?> error(String errorInfo) {
        return Ro.builder()
                .state(RoState.FAIL.getValue())
                .message(errorInfo)
                .build();
    }

    public static <T> Ro<?> success(T data) {
        return Ro.builder()
                .state(RoState.SUCCESS.getValue())
                .content(data)
                .build();
    }
    public static <T> Ro<?> success() {
        return Ro.builder()
                .state(RoState.SUCCESS.getValue())
                .build();
    }
}
